package com.xzxy.xmlg_backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xzxy.xmlg_backend.entity.Dept;
import com.xzxy.xmlg_backend.entity.Result;
import com.xzxy.xmlg_backend.service.impl.DeptServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author 周毅盛
 * @since 2025-06-10
 */
@RestController
@RequestMapping("/depts")
@Tag(name = "部门管理")
public class DeptController {


    @Autowired
    private DeptServiceImpl deptService;

    /**
     * 请求部门数据
     * @return
     */
    @GetMapping
    @Operation(summary = "获取部门列表",description = "获取所有部门的列表")
    public Result<List<Dept>> getList(){
        return Result.success(deptService.list());
    }

    /**
     * 新增部门
     */
    @PostMapping
    @Operation(summary = "新增部门",description = "新增部门信息")
    public Result add(@RequestBody Dept dept){
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        boolean b = deptService.save(dept);
        if (b)
            return Result.success();
        return Result.fail("新增部门失败，请稍后再试！");
    }

    /**
     * 修改部门信息
     */
    @PutMapping
    @Operation(summary = "修改部门",description = "修改部门信息")
    public Result update(@RequestBody Dept dept){
        dept.setUpdateTime(LocalDateTime.now());
        boolean b = deptService.updateById(dept);
        if (b)
            return Result.success();
        return Result.fail("修改部门失败，请稍后再试！");
    }

    /**
     * 删除部门信息
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除部门",description = "删除部门信息")
    public Result delete(@PathVariable Long id){
        boolean b = deptService.removeById(id);
        if (b)
            return Result.success();
        return Result.fail("删除部门失败，请稍后再试！");
    }

    /**
     * 根据id查询部门
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询部门",description = "根据ID查询部门信息")
    public Result<Dept> getById(@PathVariable Long id){
        return Result.success(deptService.getById(id));
    }



}
